﻿using System;
using System.Data;
using MyApp.DataModels;
using MyApp.IRepository;
using SqlSugar;
namespace MyApp.Repository
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        public SqlSugarScope db { get; set; }
        public BaseRepository(SqlSugarScope sqlSugarScope)
        {
            this.db = sqlSugarScope;
            db.CodeFirst.InitTables(typeof(InterfaceToolEntity));
        }
        public int Insert(TEntity entity)
        {
            return db.Insertable(entity).ExecuteCommand();
        }
        public int Delete(TEntity entity)
        {
            return db.Deleteable(entity).ExecuteCommand();
        }
        public int Update(TEntity entity)
        {
            return db.Updateable(entity).ExecuteCommand();
        }
        public TEntity Qu(TEntity entity)
        {
            db.Queryable<TEntity>().InSingle(2);
            return db.Queryable<TEntity>().First();
        }
        public TEntity QuerySingleByPK(string PK)
        {
            return db.Queryable<TEntity>().InSingle(PK);
        }
        public DataTable GetDataTable(string sql)
        {
            return db.Ado.GetDataTable(sql);
        }
        public int ExecuteCommand(string sql)
        {
            return db.Ado.ExecuteCommand(sql);
        }
    }
}
